package com.example.zlw.skindemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private static final int SD_PERMISSION_REQUEST_CODE = 1;
    private SkinFactory skinFactory;
    private static final String[] SD_PERMISSION = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        skinFactory = new SkinFactory();
        LayoutInflaterCompat.setFactory(getLayoutInflater(),skinFactory);
        SkinManager.getOutInstance().init(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
    }

    public void click(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "skin.apk");
        SkinManager.getOutInstance().loadSkin(file.getAbsolutePath());
        skinFactory.apply();
    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                //请求权限
                ActivityCompat.requestPermissions(this, SD_PERMISSION, SD_PERMISSION_REQUEST_CODE);

            }
        }
    }

}
